//
//  FoodCityViewController.m
//  FoodFamily
//
//  Created by Bang-guo on 15-6-19.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "FoodCityViewController.h"

@interface FoodCityViewController ()
@property(nonatomic, retain)CLLocationManager *locationManager;
@property(nonatomic, retain)MKMapView *mapView;
@property(nonatomic, retain)CLGeocoder *geocoder;
@end

@implementation FoodCityViewController
//-(void)dealloc {
//    [_geocoder release];
//    [_mapView release];
//    [_locationManager release];
//    
//    [super dealloc];
//    
//}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    [self.mapView removeFromSuperview];
    [self.view addSubview:mapView];
}
-(void)location {
    self.city = [[MapCityModle alloc] init];
    self.city.cityId = @"1";
    self.city.cityChineseName = @"杭州";
    [self.cityArray insertObject:self.city atIndex:0];
    self.geocoder = [[CLGeocoder alloc] init];
    

    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    
//    self.mapView = [MapViewCustom mapViewShare];
//    self.mapView.frame = CGRectMake(0, 0, WIDTH/7*6, HEIGHT/22*10);
    self.mapView.delegate = self;
    self.locationManager = [[CLLocationManager alloc] init];
    if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
        
        [self.locationManager requestWhenInUseAuthorization];
        
    }
    
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    self.mapView.mapType = MKMapTypeStandard;
    [self.view addSubview:self.mapView];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    
    if (self.cityInt == 1) {
        
        return;
    }
    //反地理编码
    CLLocation *location=[[CLLocation alloc]initWithLatitude:userLocation.location.coordinate.latitude longitude:userLocation.location.coordinate.longitude];
    self.lng =userLocation.location.coordinate.longitude;
    self.lat = userLocation.location.coordinate.latitude;
        
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark=[placemarks firstObject];
        NSString *str = [placemark.locality lowercaseString];
        NSString *subStr = [str substringToIndex:(str.length - 1)];
        NSLog(@"%@",subStr);
       self.city = [[MapCity mapCityShareDataHandle] selectMapCityValue:subStr];
        [self.cityArray replaceObjectAtIndex:0 withObject:self.city];
        [self.cityTableView reloadData];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前定位" message:self.city.cityChineseName delegate:self cancelButtonTitle:@"切换" otherButtonTitles:@"不切换", nil];
        [alert show];
//        [alert release];
    }];
    self.cityInt++;
//    [location release];
    self.mapView.showsUserLocation = NO;
    self.mapView.delegate = nil;
    [self.mapView removeFromSuperview];
    self.mapView = nil;
    
    
    
//    [_geocoder release];
//    [_mapView release];
//    [_locationManager release];

    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:self.city.cityId forKey:@"city"];
            NSLog(@"%@", self.city.cityChineseName);

        [self.MewFoodVC reloadValue];
        self.title = [NSString stringWithFormat:@"%@美食", self.city.cityChineseName];
        self.navigationItem.title = self.title;
    }

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.cityInt = 0;
    self.isSelected = YES;
//动画城市数组
    self.cityArray = [NSMutableArray arrayWithArray:[[MapCity mapCityShareDataHandle]selectMapCityAll]];
    
    [self changeCityTableView];
    [self location];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults setObject:@"1" forKey:@"city"];
    NSString *locatonCity = @"杭州";
    self.title = [NSString stringWithFormat:@"%@美食", locatonCity];
    self.navigationItem.title = self.title;

    
    
    
//    设置状态栏的背景颜色
    UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, 20)];
    statusBarView.backgroundColor=[UIColor blackColor];
    [self.navigationController.navigationBar addSubview:statusBarView];
    
    self.view.backgroundColor = [UIColor blueColor];
    
    self.navigationController.navigationBar.translucent = NO;
//    [statusBarView release];
//    切换城市的button
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"address.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    leftBarButton.tintColor = [UIColor blackColor];
    
    
    
    UIImage *image = [UIImage imageNamed:@"iconfont-fujinfuwu.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction)];
    

    
    
    self.MewFoodVC = [[NewFoodViewController alloc]init];
    [self addChildViewController:self.MewFoodVC];
    [self.view addSubview:self.MewFoodVC.view];
    self.MewFoodVC.MyDelegate = self;
    
//    [self.MewFoodVC release];

    
    
}



-(void)changeCityTableView {
    
    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(-120, 0, 120, HEIGHT - 44 - 20 - 49 ) style:UITableViewStylePlain];
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
    [self.view addSubview:self.cityTableView];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.isSelected = YES;
    //动画写法3
    [UIView beginAnimations:@"view动画开始" context:nil];
    //动画时长
    [UIView setAnimationDuration:0.5];
    self.cityTableView.frame = CGRectMake(-120, 0, 120, HEIGHT - 44 - 20 - 49);
    [UIView commitAnimations];
    
    if (indexPath.row == 0) {

        self.title = [NSString stringWithFormat:@"%@美食", self.city.cityChineseName];
        self.navigationItem.title = self.title;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        [userDefaults setObject:self.city.cityId forKey:@"city"];
    }
    
    
    if (indexPath.row > 0) {
        
        MapCityModle *city = self.cityArray[indexPath.row];
        self.title = [NSString stringWithFormat:@"%@美食", city.cityChineseName];
        self.navigationItem.title = self.title;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        [userDefaults setObject:city.cityId forKey:@"city"];

        
    }
    



    //切换城市
    [self.MewFoodVC reloadValue];
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    
    if (indexPath.row == 0) {
        MapCityModle *city = self.cityArray[indexPath.row];
        
        NSString *str = [NSString stringWithFormat:@"定位:%@", city.cityChineseName];
        cell.textLabel.text = str;
        
    } else {
        MapCityModle *city = self.cityArray[indexPath.row];
        cell.textLabel.text = city.cityChineseName;
    }
    
    return cell;
    
}


//-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//    NSArray *array = [NSArray arrayWithObjects:@"A", @"B", nil];
//    return array;
//}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"城市";
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.cityArray.count;
}

-(void)leftBarButtonAction:(UIBarButtonItem *)button {
    
    [self.view bringSubviewToFront:self.cityTableView];
    
    if (self.isSelected == NO) {
        self.isSelected = YES;
        //动画写法3
        [UIView beginAnimations:@"view动画开始" context:nil];
        //动画时长
        [UIView setAnimationDuration:0.5];
        self.cityTableView.frame = CGRectMake(-120, 0, 120, HEIGHT - 44 - 20 - 49 );
        [UIView commitAnimations];
        
    } else {
        self.isSelected = NO;
        //动画写法3
        [UIView beginAnimations:@"view动画开始" context:nil];
        //动画时长
        [UIView setAnimationDuration:0.5];
        self.cityTableView.frame = CGRectMake(0, 0, 120, HEIGHT - 44 - 20 - 49 );
        [UIView commitAnimations];
    }
    
}

-(void)rightBarAction{

    if (self.isSelected == NO) {
        self.isSelected = YES;
        //动画写法3
        [UIView beginAnimations:@"view动画开始" context:nil];
        //动画时长
        [UIView setAnimationDuration:0.5];
        self.cityTableView.frame = CGRectMake(-120, 0, 120, HEIGHT - 44 - 20 - 49 );
        [UIView commitAnimations];
        
    }
    self.restaruantsVC = [[RestaruantsNearbyViewController alloc]init];
    [self.navigationController pushViewController:self.restaruantsVC animated:YES];
    
    self.restaruantsVC.lng = self.lng;
    self.restaruantsVC.lat = self.lat;
   
   }

-(void)BringPicButton{
    if (self.isSelected == NO) {
        self.isSelected = YES;
        //动画写法3
        [UIView beginAnimations:@"view动画开始" context:nil];
        //动画时长
        [UIView setAnimationDuration:0.5];
        self.cityTableView.frame = CGRectMake(-120, 0, 120, HEIGHT - 44 - 20 - 49 );
        [UIView commitAnimations];
        
    }

}
-(void)BringUserButton{
    if (self.isSelected == NO) {
        self.isSelected = YES;
        //动画写法3
        [UIView beginAnimations:@"view动画开始" context:nil];
        //动画时长
        [UIView setAnimationDuration:0.5];
        self.cityTableView.frame = CGRectMake(-120, 0, 120, HEIGHT - 44 - 20 - 49 );
        [UIView commitAnimations];
        
    }


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
